#!/bin/bash

tee '/lib/systemd/system/add-localhost.service' <<EOF
[Unit]
Description=Adds hostname to /etc/hosts
After=network.target

[Service]
Type=oneshot
ExecStart=-/etc/add-localhost.sh

[Install]
WantedBy=default.target
EOF

mkdir -p "/etc/systemd/system/${MAIN_SERVICE}.service.d"
tee "/etc/systemd/system/${MAIN_SERVICE}.service.d/add-localhost.conf" <<'EOF'
[Unit]
After=add-localhost.service
EOF

systemctl daemon-reload
systemctl enable add-localhost.service

tee '/etc/add-localhost.sh' <<'EOF'
#!/bin/bash
echo "========================================"
echo "$(date)"

sed -ie '/127.0.0.1 ip\-/d' /etc/hosts
echo "127.0.0.1 $(hostname -s)" >> /etc/hosts;

EOF

chmod +x '/etc/add-localhost.sh'


ACCOUNT=1000

tee aa.prop <<EOF
teamcity_agent_user="$((ACCOUNT+100))"
teamcity_agent_user="$ACCOUNT"
teamcity_agent_path="/opt/buildAgent"
EOF
# teamcity_agent_user="${ACCOUNT+100}" // todo: move me back when ready


tee empty.prop <<EOF
EOF

# To overwrite an existing file (or write to a new file) that you own, substituting variable references inside the heredoc:
cat << EOF > /path/to/your/file
This line will write to the file.
${THIS} will also write to the file, with the variable contents substituted.
EOF

# To append an existing file (or write to a new file) that you own, substituting variable references inside the heredoc:
cat << FOE >> /path/to/your/file
This line will write to the file.
${THIS} will also write to the file, with the variable contents substituted.
FOE

# To overwrite an existing file (or write to a new file) that you own, with the literal contents of the heredoc:
cat << 'END_OF_FILE' > /path/to/your/file
This line will write to the file.
${THIS} will also write to the file, without the variable contents substituted.
END_OF_FILE

# To append an existing file (or write to a new file) that you own, with the literal contents of the heredoc:
cat << 'eof' >> /path/to/your/file
This line will write to the file.
${THIS} will also write to the file, without the variable contents substituted.
eof

# To overwrite an existing file (or write to a new file) owned by root, substituting variable references inside the heredoc:
cat << until_it_ends | sudo tee /path/to/your/file
This line will write to the file.
${THIS} will also write to the file, with the variable contents substituted.
until_it_ends

# To append an existing file (or write to a new file) owned by user=foo, with the literal contents of the heredoc:
cat << 'Screw_you_Foo' | sudo -u foo tee -a /path/to/your/file
This line will write to the file.
${THIS} will also write to the file, without the variable contents substituted.
Screw_you_Foo

if true ; then
    cat <<- EOF > /tmp/yourfilehere
    The leading tab is ignored.
EOF
fi

cat << EOF | fmt | mail -s "Warning: $account Exceeds Quota" $account
Your disk usage is ${usage}MB but you have only been allocated
${MAXDISKUSAGE}MB.
EOF

cat << EOF > exp || framework_failure_
  text example
EOF

cat <<EO\\F
ginstall: creating directory 'sub3'
EO\F

	cat <<-\EOF
		#
		# THIS FILE IS AUTOGENERATED;
		#
		echo "Example"
	EOF

cat <<- 'DONE' |
echo
DONE

cat >> WORKSPACE <<'EOF' || fail "Couldn't cat"
android_sdk_repository(
    name = "androidsdk",
    path = "/fake/path",
    api_level = 23,
    build_tools_version="23.0.0"
)
EOF

dist/build/shellcheck/shellcheck << 'EOF' || die "execution failed"
#!/bin/sh
echo "Hello World"
EOF